#!/usr/bin/env python

#    Copyright 2011,2012 Erik Persson
#
#    This file is part of the cell-sync-usrp project.
#
#    cell-sync-usrp is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    cell-sync-usrp is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with cell-sync-usrp.  If not, see <http://www.gnu.org/licenses/>.


import convolution
import PCCPCH_dump

def unpair(seq):
    ret = []
    for a in seq:
        ret.append(a[0])
        ret.append(a[1])
    return ret

def pair(seq):
    ret = []
    d = []
    i = 0
    for a in seq:
        d.append(a)
        if (len(d) == 2):
            ret.append(d)
            d = []
    if (len(d) != 0):
        ret.append(d)
    
    return ret
        
def main():
    conv = convolution.Convolution(9,2,[[0,2,3,4,8],[0,1,2,3,5,7,8]])
    
    seq = PCCPCH_dump.convert(PCCPCH_dump.PCCPCH_data21,PCCPCH_dump.PCCPCH_data22)
    print "length seq: " + str(len(seq)*2)
#    print "Length data8: " + str(len(PCCPCH_dump.PCCPCH_data10))
#    print "Length data8: " + str(len(PCCPCH_dump.PCCPCH_data11))
    
    
    arr = []
    arr.append(PCCPCH_dump.PCCPCH_data31)
    arr.append(PCCPCH_dump.PCCPCH_data32)
    arr.append(PCCPCH_dump.PCCPCH_data33)
    arr.append(PCCPCH_dump.PCCPCH_data34)
    arr.append(PCCPCH_dump.PCCPCH_data35)
    arr.append(PCCPCH_dump.PCCPCH_data36)
    arr.append(PCCPCH_dump.PCCPCH_data37)
    arr.append(PCCPCH_dump.PCCPCH_data38)
    arr.append(PCCPCH_dump.PCCPCH_data39)
    #arr.append(PCCPCH_dump.PCCPCH_data30)
    arr = PCCPCH_dump.PCCPCH
    

    mapping = [0, 30, 60, 90, 120, 150, 180, 210, 240, 20, 50, 80, 110, 140, 170, 200, 230, 260, 10, 40, 70, 100, 130, 160, 190, 220, 250, 5, 35, 65, 95, 125, 155, 185, 215, 245, 15, 45, 75, 105, 135, 165, 195, 225, 255, 25, 55, 85, 115, 145, 175, 205, 235, 265, 3, 33, 63, 93, 123, 153, 183, 213, 243, 13, 43, 73, 103, 133, 163, 193, 223, 253, 23, 53, 83, 113, 143, 173, 203, 233, 263, 8, 38, 68, 98, 128, 158, 188, 218, 248, 18, 48, 78, 108, 138, 168, 198, 228, 258, 28, 58, 88, 118, 148, 178, 208, 238, 268, 1, 31, 61, 91, 121, 151, 181, 211, 241, 11, 41, 71, 101, 131, 161, 191, 221, 251, 21, 51, 81, 111, 141, 171, 201, 231, 261, 6, 36, 66, 96, 126, 156, 186, 216, 246, 16, 46, 76, 106, 136, 166, 196, 226, 256, 26, 56, 86, 116, 146, 176, 206, 236, 266, 4, 34, 64, 94, 124, 154, 184, 214, 244, 14, 44, 74, 104, 134, 164, 194, 224, 254, 24, 54, 84, 114, 144, 174, 204, 234, 264, 19, 49, 79, 109, 139, 169, 199, 229, 259, 9, 39, 69, 99, 129, 159, 189, 219, 249, 29, 59, 89, 119, 149, 179, 209, 239, 269, 12, 42, 72, 102, 132, 162, 192, 222, 252, 2, 32, 62, 92, 122, 152, 182, 212, 242, 7, 37, 67, 97, 127, 157, 187, 217, 247, 22, 52, 82, 112, 142, 172, 202, 232, 262, 27, 57, 87, 117, 147, 177, 207, 237, 267, 17, 47, 77, 107, 137, 167, 197, 227, 257]
    #    [0, 30, 60, 90, 120, 20, 50, 80, 110, 10, 40, 70, 100, 130, 5, 35, 65, 95, 125, 15, 45, 75, 105, 25, 55, 85, 115, 3, 33, 63, 93, 123, 13, 43, 73, 103, 133, 23, 53, 83, 113, 8, 38, 68, 98, 128, 18, 48, 78, 108, 28, 58, 88, 118, 1, 31, 61, 91, 121, 11, 41, 71, 101, 131, 21, 51, 81, 111, 6, 36, 66, 96, 126, 16, 46, 76, 106, 26, 56, 86, 116, 4, 34, 64, 94, 124, 14, 44, 74, 104, 134, 24, 54, 84, 114, 19, 49, 79, 109, 9, 39, 69, 99, 129, 29, 59, 89, 119, 12, 42, 72, 102, 132, 2, 32, 62, 92, 122, 7, 37, 67, 97, 127, 22, 52, 82, 112, 27, 57, 87, 117, 17, 47, 77, 107]
    mapping2 = [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 142, 144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180, 182, 184, 186, 188, 190, 192, 194, 196, 198, 200, 202, 204, 206, 208, 210, 212, 214, 216, 218, 220, 222, 224, 226, 228, 230, 232, 234, 236, 238, 240, 242, 244, 246, 248, 250, 252, 254, 256, 258, 260, 262, 264, 266, 268, 270, 272, 274, 276, 278, 280, 282, 284, 286, 288, 290, 292, 294, 296, 298, 300, 302, 304, 306, 308, 310, 312, 314, 316, 318, 320, 322, 324, 326, 328, 330, 332, 334, 336, 338, 340, 342, 344, 346, 348, 350, 352, 354, 356, 358, 360, 362, 364, 366, 368, 370, 372, 374, 376, 378, 380, 382, 384, 386, 388, 390, 392, 394, 396, 398, 400, 402, 404, 406, 408, 410, 412, 414, 416, 418, 420, 422, 424, 426, 428, 430, 432, 434, 436, 438, 440, 442, 444, 446, 448, 450, 452, 454, 456, 458, 460, 462, 464, 466, 468, 470, 472, 474, 476, 478, 480, 482, 484, 486, 488, 490, 492, 494, 496, 498, 500, 502, 504, 506, 508, 510, 512, 514, 516, 518, 520, 522, 524, 526, 528, 530, 532, 534, 536, 538, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99, 101, 103, 105, 107, 109, 111, 113, 115, 117, 119, 121, 123, 125, 127, 129, 131, 133, 135, 137, 139, 141, 143, 145, 147, 149, 151, 153, 155, 157, 159, 161, 163, 165, 167, 169, 171, 173, 175, 177, 179, 181, 183, 185, 187, 189, 191, 193, 195, 197, 199, 201, 203, 205, 207, 209, 211, 213, 215, 217, 219, 221, 223, 225, 227, 229, 231, 233, 235, 237, 239, 241, 243, 245, 247, 249, 251, 253, 255, 257, 259, 261, 263, 265, 267, 269, 271, 273, 275, 277, 279, 281, 283, 285, 287, 289, 291, 293, 295, 297, 299, 301, 303, 305, 307, 309, 311, 313, 315, 317, 319, 321, 323, 325, 327, 329, 331, 333, 335, 337, 339, 341, 343, 345, 347, 349, 351, 353, 355, 357, 359, 361, 363, 365, 367, 369, 371, 373, 375, 377, 379, 381, 383, 385, 387, 389, 391, 393, 395, 397, 399, 401, 403, 405, 407, 409, 411, 413, 415, 417, 419, 421, 423, 425, 427, 429, 431, 433, 435, 437, 439, 441, 443, 445, 447, 449, 451, 453, 455, 457, 459, 461, 463, 465, 467, 469, 471, 473, 475, 477, 479, 481, 483, 485, 487, 489, 491, 493, 495, 497, 499, 501, 503, 505, 507, 509, 511, 513, 515, 517, 519, 521, 523, 525, 527, 529, 531, 533, 535, 537, 539]
    #    [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 142, 144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180, 182, 184, 186, 188, 190, 192, 194, 196, 198, 200, 202, 204, 206, 208, 210, 212, 214, 216, 218, 220, 222, 224, 226, 228, 230, 232, 234, 236, 238, 240, 242, 244, 246, 248, 250, 252, 254, 256, 258, 260, 262, 264, 266, 268, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99, 101, 103, 105, 107, 109, 111, 113, 115, 117, 119, 121, 123, 125, 127, 129, 131, 133, 135, 137, 139, 141, 143, 145, 147, 149, 151, 153, 155, 157, 159, 161, 163, 165, 167, 169, 171, 173, 175, 177, 179, 181, 183, 185, 187, 189, 191, 193, 195, 197, 199, 201, 203, 205, 207, 209, 211, 213, 215, 217, 219, 221, 223, 225, 227, 229, 231, 233, 235, 237, 239, 241, 243, 245, 247, 249, 251, 253, 255, 257, 259, 261, 263, 265, 267, 269]

    i=0
    print "decodedSeq = []"
    while i<len(arr)-1:
        conv._clearBuffer()
        
        # first deinterleaving
        tmp1 = [5] * 270
        tmp2 = [5] * 270   
        j = 0
        while (j<270):
            tmp1[mapping[j]] = arr[i][j]
            tmp2[mapping[j]] = arr[i+1][j]
            j+=1
#        print (tmp1)
#        print (tmp2)
        seq = PCCPCH_dump.convert(tmp1,tmp2)
        seq = unpair(seq)
        
        #second deinterleaving
        tmp3 = [0] * 270*2
        j = 0
        while (j<270*2):
            mapp = mapping2[j]
            data = seq[j]
            tmp3[mapp] = data
            j+=1
        tmp3 = pair(tmp3)
#        print tmp3
        decoded2 = conv.decodeSeq(tmp3,270-8)
        print "decodedSeq.append(" + str(decoded2) + ")"
        
        tmp3 = unpair(tmp3)
    
        
#        print "    tmp1 count: " +str(tmp1.count("0"))
#        print "    tmp2 count: " +str(tmp2.count("0"))
#        print "    tmp3 count: " +str(tmp3.count(0))
#        print "     seq count: " +str(seq.count(0))
#        print "  arr[i] count: " +str(arr[i].count("0"))
#        print "arr[i+1] count: " +str(arr[i+1].count("0"))
#        
        i+=2
    
    return
    
    
    decoded1 = conv.decodeSeq(seq)
    print "Decoded sequence1: " + str(decoded1)
    
    conv._clearBuffer()
    seq = PCCPCH_dump.convert(PCCPCH_dump.PCCPCH_data22,PCCPCH_dump.PCCPCH_data23)
    decoded2 = conv.decodeSeq(seq)
    print "Decoded sequence2: " + str(decoded2)

    conv._clearBuffer()
    seq = PCCPCH_dump.convert(PCCPCH_dump.PCCPCH_data23,PCCPCH_dump.PCCPCH_data24)
    decoded3 = conv.decodeSeq(seq)
    print "Decoded sequence3: " + str(decoded3)
    
    conv._clearBuffer()
    seq = PCCPCH_dump.convert(PCCPCH_dump.PCCPCH_data24,PCCPCH_dump.PCCPCH_data25)
    decoded4 = conv.decodeSeq(seq)
    print "Decoded sequence4: " + str(decoded4)
    
    
    print "decoded1 == decoded 2: " + str(decoded1==decoded2)
    
    l = len(decoded1)
    i=0
    match=0
    while (i<l):
        if (decoded1[i] == decoded2[i]):
            match+=1
        
        i+=1 
    
    print "Match (%): " + str(float(match)/float(l)*100)
    print "Match: " + str(match)
    print "Length: " + str(len(decoded1))
if __name__ == '__main__':
    main()
    
    
